//
// Created by yunnan xu on 2020/12/10.
//

#include "CPPUtils.h"

void StringSplit(const std::string & s, std::vector<std::string> & tokens, const std::string & delimiters)
{
    std::string::size_type pos1, pos2;

    size_t len = s.length();

    pos2 = s.find(delimiters);

    pos1 = 0;

    while(std::string::npos != pos2)
    {
        tokens.emplace_back(s.substr(pos1, pos2-pos1));
        pos1 = pos2 + delimiters.size();
        pos2 = s.find(delimiters, pos1);
    }

    if(pos1 != len)
        tokens.emplace_back(s.substr(pos1));
}

bool StringEndsWith(const std::string & str, const std::string & suffix)
{
    int str_len = str.length();
    int suffix_len = suffix.length();

    if (suffix_len > str_len) return false;

    for(int i = 0; i < suffix_len; i ++)
    {
        if (str[str_len - 1 - i] != suffix[suffix_len - 1 - i])
        {
            return false;
        }
    }
    return true;
}